<template>
    <div>
        <el-drawer :title="title" visible @close="close()" size="90%" :wrapperClosable="false">
            <div class="add-main" v-loading="loading">
                <div class="btn-con" v-if="handleShow == 'add' || handleShow == 'edit'">
                    <el-button type="primary" size="small" icon="el-icon-circle-check" @click.native.prevent="submit()" :disabled="loading">提交
                    </el-button>
                </div>
                <div class="btn-con" v-if="submitShow">
                    <el-button type="primary" size="small" icon="el-icon-circle-check" @click.native.prevent="submit0()">提交上架
                    </el-button>
                </div>
                <basic ref="basic" :handleShow="handleShow" :data="data" />
            </div>
        </el-drawer>
    </div>
</template>

<script>
import basic from "./detail/basic"
import throttle from 'lodash.throttle'
import { setGoodsInfo, putGoodsInfo } from "@/api/goods/goods"
import { del } from 'vue';
export default {
    name: '',
    props: {
        title: String,
        submitShow: Boolean,
        handleShow: {
            type: String, //add新增edit编辑
            default: ""
        },
        data: {
            type: Object,
            default: function () {
                return {}
            }
        }
    },
    components: {
        basic,
    },
    data() {
        return {
            loading: false,
        }
    },
    created() {
        console.log(this.handleShow, '+++')
        console.log(this.submitShow, '000')
        console.log(this.data, '商品详情')
    },
    methods: {
        close() {
            this.$refs.basic.tableColumnList = {
                tableHeaderList: [],
                tableBodyList: []
            }
            this.$refs.basic.privateGoodsItem = [
                {
                    privateSpecName: '', //规格名
                    dynamicTags: [], //规格值数组
                    // inputVisible: false,
                    // inputValue: ''
                }
            ]
            this.$emit('close')
            this.$emit('load')
        },
        submit: throttle(function () {
            let fun1 = new Promise((resolve, reject) => {
                this.$refs.basic.$refs.form.validate((valid) => {
                    if (valid) {
                        resolve();
                    } else reject();
                });
            });
            Promise.all([fun1]).then(data => {
                this.loading = true;
                let basic = this.$refs.basic
                console.log(basic.privateGoodsItem, 666)
                console.log(basic.privateGoodsItem[0], 789)
                let form = {
                    ...basic.form,
                    table_column: (basic.tableColumnList.tableBodyList.length > 0 && basic.tableColumnList.tableHeaderList.length > 0) ? JSON.stringify(basic.tableColumnList) : null,
                    private_goods: basic.privateGoodsItem.length > 0 && basic.privateGoodsItem[0].dynamicTags.length > 0 ? JSON.stringify(basic.privateGoodsItem) : null,
                }
                form.cateids = form.cate_ids[1]
                form.slider = form.slider0
                form.code = this.data ? this.data.code : null
                console.log(form, "formm")
                delete form.cate_ids
                delete form.cates
                // delete form.slider0
                // delete form.cate_name
                delete form.items
                setGoodsInfo(form).then(res => {
                    this.msgSuccess("操作成功！");
                    this.close()
                    this.loading = false;
                }).catch(() => {
                    this.loading = false;
                })
            })
        }, 1000, { trailing: false }),

        submit0: throttle(function () {
            let fun1 = new Promise((resolve, reject) => {
                this.$refs.basic.$refs.form.validate((valid) => {
                    if (valid) {
                        resolve();
                    } else reject();
                });
            });
            Promise.all([fun1]).then(data => {
                this.loading = true;
                let basic = this.$refs.basic
                console.log(basic.privateGoodsItem, 666)
                console.log(basic.privateGoodsItem[0], 789)
                let form = {
                    ...basic.form,
                    table_column: (basic.tableColumnList.tableBodyList.length > 0 && basic.tableColumnList.tableHeaderList.length > 0) ? JSON.stringify(basic.tableColumnList) : null,
                    private_goods: basic.privateGoodsItem.length > 0 && basic.privateGoodsItem[0].dynamicTags.length > 0 ? JSON.stringify(basic.privateGoodsItem) : null,
                }
                form.cateids = form.cate_ids[1]
                form.slider = form.slider0
                form.code = this.data ? this.data.code : null
                putGoodsInfo(form).then(res => {
                    this.msgSuccess("操作成功！");
                    this.close()
                    this.loading = false;
                })
            })
        }, 1000, { trailing: false }),
    },
}

</script>
<style scoped lang='scss'>
/deep/.detail-type {
    .tit {
        font-size: 14px;
        color: #666666;
        font-weight: 600;
        border-left: 5px solid #409eff;
        padding-left: 8px;
        height: 14px;
        line-height: 14px;
    }
}
</style>